import {Component, OnInit} from '@angular/core';
import {FetchService} from "../../../../core/net/fetch.service";
import {DomSanitizer, SafeResourceUrl} from "@angular/platform-browser";
import {format, subDays} from 'date-fns';

@Component({
  selector: 'app-report-sqyb',
  templateUrl: './sqyb.component.html',
  styleUrls: ['../report.component.scss', './sqyb.component.scss']
})
export class SqybComponent implements OnInit {
  yearValue = 2020;
  monthPickerValue = new Date().getMonth();
  serverUrl = (window as any).environment.reportUrl;
  iframeSrc: SafeResourceUrl;

  range = ["", new Date()];


  constructor(
    private fetchService: FetchService,
    private sanitizer: DomSanitizer
  ) {
  }

  ngOnInit() {
    if (parseFloat( format(this.range[1], 'MM')) > 9) {
     this. range = ["2021-10-1", new Date()];
    } else {
      this. range = ["2021-1-1", new Date()];
    }
    this.queryInfo();
  }

  queryInfo(): void {
    const year = this.yearValue;
    let beginMonth;
    let endMonth;
    let time;
    if (this.monthPickerValue < 12) {
      beginMonth = this.monthPickerValue + 1;
      endMonth = this.monthPickerValue + 1;
      time = `${year}-${beginMonth}`;
    } else if (this.monthPickerValue == 13) {
      beginMonth = 4;
      endMonth = 9;
      time = `${year}-夏秋灌`;
    } else if (this.monthPickerValue == 14) {
      beginMonth = 10;
      endMonth = 11;
      time = `${year}-冬灌`;
    } else if (this.monthPickerValue == 15) {
      beginMonth = 4;
      endMonth = 11;
      time = `${year}`;
    }
    const beginTime = format(this.range[0], 'YYYY-MM-DD');
    const endTime = format(this.range[1], 'YYYY-MM-DD');
    const token = localStorage.token;
    const link = `${this.serverUrl}preview?_u=wrs:水情月报报表.ureport.xml&_t=3,6&token=${token}&beginTime=${beginTime}&endTime=${endTime}`;
    this.iframeSrc = this.sanitizer.bypassSecurityTrustResourceUrl(link);
  }


  dateChange(e): void {
    this.queryInfo();
  }

}

